import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/tools/test'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },
  {
    path: '/product',
    component: Layout,
    redirect: '/product/category',
    meta: { title: '商品管理', icon: 'dashboard' },
    children: [
      {
        path: 'category',
        name: 'category',
        component: () => import('@/views/product/category'),
        meta: { title: '分类管理', icon: 'dashboard' }
      },
      {
        path: 'brand',
        name: 'brand',
        component: () => import('@/views/product/brand/brand'),
        meta: { title: '品牌管理', icon: 'dashboard' }
      },
      {
        path: 'brand/editor',
        name: 'brand_add',
        component: () => import('@/views/product/brand/brand_editor'),
        meta: { title: '品牌新增', icon: 'dashboard' },
        hidden: true
      },
      {
        path: 'brand/editor/:id',
        name: 'brand_update',
        component: () => import('@/views/product/brand/brand_editor'),
        meta: { title: '品牌修改', icon: 'dashboard' },
        hidden: true
      }
    ]
  },
  {
    path: '/rbac',
    component: Layout,
    redirect: '/rbac/menu',
    meta: { title: '权限管理', icon: 'menu' },
    children: [
      {
        path: 'menu',
        name: 'menu',
        component: () => import('@/views/rbac/menu'),
        meta: { title: '菜单管理', icon: 'menu' }
      },
      {
        path: 'menu_permission',
        name: 'menu_permission',
        component: () => import('@/views/rbac/menu-permission'),
        meta: { title: '资源管理', icon: 'menu' }
      },
      {
        path: 'shop_menu',
        name: 'shop_menu',
        component: () => import('@/views/rbac/shop-menu'),
        meta: { title: '店铺菜单管理', icon: 'menu' }
      },
      {
        path: 'shop_menu_permission',
        name: 'shop_menu_permission',
        component: () => import('@/views/rbac/shop-menu-permission'),
        meta: { title: '店铺资源管理', icon: 'menu' }
      },

      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/rbac/role'),
        meta: { title: '平台角色管理', icon: 'menu' }
      },
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
